
window.addEvent('domready',function(){
	interfaceCtrl = new InterfaceCtrl();
	dataCommunication = new DataCommunication();
	formBuilder = new FormBuilder();
	formAction = new FormAction();
	userCtrler = new UserCtrler();
	listCtrler = new ListCtrler();
	//用户信息读取完成后开始生成列表信息
	userCtrler.addEvent('onComplete', function(){
		listCtrler.showListTable();
	}).build().injectTop(document.body);
});

var ListCtrler = new Class({
	listPars : {
		//搜索关键字
		'keyword' : '',
		//当前页数
		'page'    : 1,
		//排序规则
		'orderby' : ''
	},
	//初始化列表控制器
	initialize : function(){
		//列表高级菜单控件控制器
		this.advancedMenu =  new AdvancedMenu();
		//列表table控件控制器
		this.listTable =  new ListTable($('pageBase'));
		//导航条控件
		this.navigation = new Navigation();
	},
	
	//显示条目列表控件table
	showListTable : function(){
		this.listTable.buildTable()
		.setStyle('display', 'none')
		.injectInside(this.listTable.tableArea);
		this.listTable.addEvent('onOrder', function(orderBy, callElmt){
			this.listPars.orderby = (this.listPars.orderby == orderBy ? '' : orderBy);
			if (this.orderElmt && this.orderElmt != callElmt) this.orderElmt.toggleClass('order');
			this.orderElmt = callElmt.toggleClass('order');
			this.refreshListTable();
		}.bind(this));
		this.advancedMenu.build().injectBefore(this.listTable.table);
		this.advancedMenu.addEvent('onSearch', function(keyword){
			this.listPars.keyword = keyword;
			this.listPars.page = 1;
			this.refreshListTable();
		}.bind(this));
		this.advancedMenu.addEvent('onEmptySearch', function(){
			if (!this.listPars.keyword) return alert('请输入搜索关键字');
			this.listPars.keyword = '';
			this.refreshListTable();
		}.bind(this));
		this.advancedMenu.addEvent('onCreate', function(){
			this.listTable.action.item(false, {'id': 0, 'action': 'create'});
		}.bind(this));
		userCtrler.addEvent('onStateChange', function(){
			this.advancedMenu.refresh();
		}.bind(this));
		if (!$chk(this.listInfoElmt)) {
			this.listInfoElmt = new Element('div',{
				'class'	: 'listInfo'
			})
		}
		this.listInfoElmt.injectInside(this.listTable.tableArea).setText('列表读取中…');
		dataCommunication.getPeopleList(this.listPars, function(result){
			try{
				var data = Json.evaluate(result);
				if (!data.isSucceed) {
					this.listInfoElmt.setText(data.message);
					return false;
				} 
				this.pushPeopleListData(data.list, this.listInfoElmt);
				this.listTable.table.setStyle('display', '');
			}catch(e){
				this.listInfoElmt.setHTML('资料解析失败：'+e.message);
			}
			this.navigation.build(data.page).injectAfter(this.listTable.table);
			this.navigation.addEvent('onJumpPage', function(pageNumber){
				this.listPars.page = pageNumber;
				this.refreshListTable();
			}.bind(this));
		}.bind(this),this.listInfoElmt);
	},
	
	//刷新条目列表控件table
	refreshListTable : function(id){
		this.listTable.clear();
		this.listTable.table.setStyle('display', 'none');
		this.navigation.clear();
		if (!$chk(this.listInfoElmt)) {
			this.listInfoElmt = new Element('div',{
				'class'	: 'listInfo'
			});
		}
		this.listInfoElmt.injectInside(this.listTable.tableArea).setText('列表读取中…');
		dataCommunication.getPeopleList(this.listPars, function(result){
			var data = Json.evaluate(result);
			if (!data.isSucceed) {
				this.listInfoElmt.setText(data.message);
				return false;
			} 
			this.pushPeopleListData(data.list, this.listInfoElmt);
			this.listTable.table.setStyle('display', '');
			this.navigation.build(data.page).injectAfter(this.listTable.table);
			if (!id) return;
			var openedTr = $E('#'+id, this.listTable.table);
			this.listTable.markOpened(openedTr);
			this.showMoreBox(openedTr, id);
		}.bind(this),this.listInfoElmt);
	},
	
	//根据服务器返回值生成并填充条目列表控件table中的内容
	pushPeopleListData : function(dataArray, infoElmt) {
		try {
			dataArray.each(function(data){
				var newListTr = this.listTable.buildListTr()
				.addEvent('click', this.showMoreBox.create({'event':true, 'arguments':data.id, 'bind':this}))
				.setProperty('id', data.id)
				.setName(data.name)
				.setBrief(data.brief || '未定义联系信息…')
				.injectInside(this.listTable.table.getElement('tbody'));
			},this);
		} catch (e){
			infoElmt.setHTML('资料解析失败：'+e.message);
			return false;
		}
		infoElmt.remove();
		return true;
	},
	
	//显示详细信息控件
	showMoreBox : function(e, id){
		if ($type(e) == 'event') e = new Event(e).target.getParent();
		if (this.listTable.menuBoxTr) this.listTable.menuBoxTr.remove();
		if (this.listTable.moreBoxTr) this.listTable.moreBoxTr.remove();
		this.listTable.openedTrElmt = e;
		var moreBoxTr = this.listTable.buildMoreBoxTr().injectAfter(e);
		var menuBoxTr = this.listTable.buildMenuBoxTr(id, membership.popedom.linkman).injectAfter(e);
		var moreBoxInfoElmt = new Element('div',{
			'class' : 'infoArea'
		}).injectInside(this.listTable.moreBoxTd);
		moreBoxInfoElmt.setHTML('资料下载中…');
		
		dataCommunication.getMoreData(id, function(result){
			try {
				var data = Json.evaluate(result);
				if (!data.isSucceed) {
					moreBoxInfoElmt.setText(data.message);
					return false;
				} 
				this.pushMoreData(data.data, moreBoxInfoElmt);
				this.openedId = id;
			}catch(e){
				moreBoxInfoElmt.setHTML('资料解析失败：'+e.message);
			}
		}.bind(this),moreBoxInfoElmt);
	},

	//根据给定数据包生成并填充moreBoxTd(详细信息控件)中的内容
	pushMoreData : function(moreData, infoElmt) {
		try	{
			for ( var className in moreData )	{
				this.listTable.moreBoxTr.pushClassData(moreData[className]).injectInside(this.listTable.moreBoxTd);
			}
		} catch (e){
			infoElmt.setHTML('资料解析失败：'+e.message);
			return false;
		}
		infoElmt.remove();
		return true;
	}

});